package org.liaohailong.victorlibrary.ui

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.view.View
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity

/**
 * E-mail: liaohailong190@foxmail.com
 * Date: 2020/7/8 14:40
 * Description:
 * 横竖屏切换界面
 * @author liaohailong
 */
class OrientationChangedActivity : BaseActivity(), View.OnClickListener {

    companion object {
        private const val TAG = "OrientationChanged"
        fun show(context: Context) {
            val intent = Intent(context, OrientationChangedActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(TAG, "onCreate called")
        setContentView(R.layout.activity_orientation_changed)
        initView()
    }

    private fun initView() {
        findViewById<View>(R.id.btn_switch).setOnClickListener(this)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // 横竖屏切换，替换布局
        setContentView(R.layout.activity_orientation_changed)
        initView()
        Log.i(TAG, "orientation = ${newConfig.orientation}")
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_switch -> {
                requestedOrientation = when (resources.configuration.orientation) {
                    // 横屏切换使用带传感器模式的，可以支持180切换（上下颠倒）
                    Configuration.ORIENTATION_PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
                    Configuration.ORIENTATION_LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                    else -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                }
            }
        }
    }
}